package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.exception.SetmealEnableFailedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Slf4j

public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    @Autowired
    private DishMapper dishMapper;
    @Override
    public PageResult findMealQueryPage(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getPage(),setmealPageQueryDTO.getPageSize());

        List<SetmealVO> setmealVOS = setmealMapper.select(setmealPageQueryDTO);
        Page<SetmealVO>page= (Page<SetmealVO>)setmealVOS;
        return new PageResult(page.getTotal(),page.getResult());
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void startOrStop(Integer status, Long id) {

        //如果套餐中存在未起售的菜品，则套餐无法起售
        // 在关系表中通过套餐ID，查找菜品id,
        List<Long> dishId = setmealDishMapper.findDishIdBySetmealId(id);
        //在菜品表中查找菜品的状态，如果为0，则直接抛出异常
        for (Long aLong : dishId) {
            Dish dish = dishMapper.select(aLong);
            if (dish.getStatus()==StatusConstant.DISABLE)
                throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
        }
         Setmeal setmeal = Setmeal.builder()
                        .status(status)
                        .id(id)
                        .build();
        setmealMapper.update(setmeal);

    }

    @Override
    public void add(SetmealDTO setmealDTO) {
        //一个套餐可以有多个菜品，一个菜品可以属于多个套餐中，所以是一个多对多型关系，这时候想要操作套餐表，需要找关系表
        //先将套餐信息插入套餐表
        Setmeal setmeal=new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.insert(setmeal);
        Long id = setmeal.getId();

        //再将关系信息插入关系表
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        if (setmealDishes==null&&setmealDishes.size()==0)return;
        else {
            setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(id));

            for (SetmealDish setmealDish : setmealDishes) {
              setmealDishMapper.insert(setmealDish);
            }
        }
        /*
        * setmealDishes	object []	必须		套餐包含的菜品
item 类型: object

├─ copies	integer	必须		份数
format: int32

├─ dishId	integer	必须		菜品id
format: int64

├─ id	integer	非必须		套餐和菜品关系id
format: int64

├─ name	string	必须		菜品名称
├─ price	number	必须		菜品价格
├─ setmealId	integer	必须		套餐id
format: int64*/


    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(Long[] ids) {
        //如果套餐正在起售，则拒绝，
        for (Long id : ids) {
            Setmeal setmeal = setmealMapper.selectById(id);
            Integer status = setmeal.getStatus();
            if (status== StatusConstant.ENABLE)
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
        }

        //删除套餐，还要把关系表中的含有套餐id的信息删除
            setmealMapper.delete(ids);
            setmealDishMapper.deleteDishIdBySetmealId(ids);

    }

    @Override
    public SetmealVO findByid(Long id) {
        //首先从Setmeal中查找信息
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO=new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //然后再从关系表中找到信息封装
        List<SetmealDish> bySetmealId = setmealDishMapper.findBySetmealId(id);
        setmealVO.setSetmealDishes(bySetmealId);
        return setmealVO;
    }

    @Override
    @Transactional
    public void update(SetmealDTO setmealDTO) {
        //修改逻辑：
        //修改套餐表
        Setmeal setmeal= new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.update(setmeal);
        //修改关系表逻辑，先把里面的删除，然后再插入
        Long[] id={setmeal.getId()};
        setmealDishMapper.deleteDishIdBySetmealId(id);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmeal.getId()));
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDishMapper.insert(setmealDish);
        }
    }


}
